﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class page_Supplier_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ReadAction();
            SetPageNoCache();

            if (this.hAction.Value == "Edit")
            {
                bindSupplier();
                this.lblInfo.Text = "编辑";
            }
            else
            {
                this.lblInfo.Text = "添加";
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.txtSupplierName.Text == "")
        {
            Shove._Web.JavaScript.Alert(this.Page,"供应商名字不能为空");
            return;
        }

        DAL.Tables.tbl_Shop_Supplier Supplier = new DAL.Tables.tbl_Shop_Supplier();

        Supplier.SupplierName.Value = this.txtSupplierName.Text;
        Supplier.Address.Value = this.txtAddress.Text;
        Supplier.Contact.Value = this.txtContact.Text;
        Supplier.Remark.Value = this.txtRemark.Text;
        Supplier.Tel.Value = this.txtTel.Text;
        Supplier.Isuse.Value = true;

        if (chk.Checked == true)
        {
            Supplier.ShopID.Value = _Site.ID;
        }
        else
        {
            Supplier.ShopID.Value = -1;
        }

        if (this.hAction.Value == "Add")                      //添加操作 
        {
            if (Supplier.Insert()>=0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "添加成功！", "Supplier_Manager.aspx");
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "失败");
            }
        }
        else if (this.hAction.Value == "Edit")             //修改操作
        {
            int supplierId = Shove._Convert.StrToInt(this.hMasterID.Value, 1);

            if (Supplier.Update("ID="+supplierId)>=0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "修改成功！", "Supplier_Manager.aspx");
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "失败");
            }
        }
    }

    private void ReadAction()
    {
        String sAction = Shove._Web.Utility.GetRequest("Action");
        this.hAction.Value = sAction;
        try
        {
            String sID =Shove._Web.Utility.GetRequest("ID");
            this.hMasterID.Value = sID;
        }
        catch
        {

        }
    }

    //当用户做的是编辑操作时,显示要编辑的供应商的所有信息
    private void bindSupplier()
    {
        string sID = this.hMasterID.Value;
        if (Shove._Convert.StrToInt(sID,0) != 0)
        {
            DAL.Tables.tbl_Shop_Supplier Supplier = new DAL.Tables.tbl_Shop_Supplier();
            DataTable dtSupplier = Supplier.Open("", "ID="+sID, "");
            if (dtSupplier != null)
            {
                this.txtAddress.Text = dtSupplier.Rows[0]["Address"].ToString();
                this.txtSupplierName.Text = dtSupplier.Rows[0]["SupplierName"].ToString();
                this.txtContact.Text = dtSupplier.Rows[0]["Contact"].ToString();
                this.txtTel.Text = dtSupplier.Rows[0]["Tel"].ToString();
                this.txtRemark.Text = dtSupplier.Rows[0]["Remark"].ToString();
            }
        }
    }

    //取消添加或者编辑时　　返回到管理页面
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        Response.Redirect("Supplier_Manager.aspx");
    }

    /// </summary>
    /// 设置页面不被缓存
    /// </summary>
    public void SetPageNoCache()
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.AddHeader("pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("cache-control", "private");
        HttpContext.Current.Response.CacheControl = "no-cache";
    }
}
